﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OCR.SystemThread
{
    class DelayNgSingleThread
    {
        public static bool DelayNgSingleThread_Flowrec = false;
        public static Thread DelayNgSingleFlow_Thread = null;

        public static int iDelayNgSingle_Step = 0;

        public static bool DelayNgSingleThread_Pause = false;

        public static DateTime startDt;

        public static ConcurrentQueue<bool> infoQueue = new ConcurrentQueue<bool>();
        public static ConcurrentQueue<bool> bufferQueue = new ConcurrentQueue<bool>();


        public static int _delayCount = 3;

        public static void DelayNgSingle_Thread()
        {
            iDelayNgSingle_Step = 0;
            DelayNgSingleThread_Pause = false;
            DelayNgSingleThread_Flowrec = true;
            DelayNgSingleFlow_Thread = new Thread(DelayNgSingleThreadFlow);
            DelayNgSingleFlow_Thread.IsBackground = true;
            DelayNgSingleFlow_Thread.Start();
            Console.WriteLine("----DelayNgSingleThread启动");
        }

        private static void DelayNgSingleThreadFlow()
        {
            while (DelayNgSingleThread_Flowrec)
            {
                if (!DelayNgSingleThread_Pause)
                {
                    switch (iDelayNgSingle_Step)
                    {
                        case 0:
                            if (infoQueue.TryDequeue(out bool value))
                            {
                                Console.WriteLine("infoQueue输出:" + value);
                                bufferQueue.Enqueue(value);
                                Console.WriteLine("bufferQueue添加:" + value);
                                // 当缓冲区达到延迟数量时，输出最早的值
                                if (bufferQueue.Count >= _delayCount)
                                {
                                    Console.WriteLine("bufferQueue达到延迟数量");
                                    bufferQueue.TryDequeue(out bool delayedValue);
                                    Console.WriteLine("bufferQueue输出:" + delayedValue);
                                    OutputDelayNgSingleThread.outputQueue.Enqueue(delayedValue);
                                    Console.WriteLine("outputQueue添加:" + delayedValue);
                                }
                            }
                            break;
                        case 5:

                            break;
                        case 10:

                            break;

                        case 15:

                            break;
                        case 20:

                            break;
                        case 25:

                            break;
                    }
                }


            }

        }
    }
}
